/*业务逻辑(左侧加号展开阅读):
 *	1.获取前台用户填写的注册信息
 *	2.检验数据合法性
 *		1) 检验是否有输入空值			错误代号：3
 *		2) 检验两次密码是否一致		错误代号：2
 *		3) 检验该手机号是否已注册		错误代号：1
 *		4) 系统抛出的其他未知异常		错误代号：0
 *	  将错误代号传至fail_to_register.jsp，输出错误信息。
 *	3.连接数据库，添加用户表的用户信息
 *	4.在“猜你喜欢”表中添加该用户对5种数据类型的统计
 * 
 * 
 * 
 */


package OSMS.action.User;

import OSMS.dao.CustomerDao;
import OSMS.entity.Customers;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.swing.*;
import java.io.IOException;
import java.util.List;

public class Cus_Register_Servlet extends HttpServlet {

	CustomerDao cdao = new CustomerDao();
    boolean flag = false;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");

        try {
            // 1. 获取页面数据
            String cname = request.getParameter("name").trim();
            String tel = request.getParameter("tel").trim();
            String sex = request.getParameter("sex").trim();
            String address = request.getParameter("address").trim();
            String cpwd = request.getParameter("password").trim();
            String cpwd_assure = request.getParameter("password_again").trim();

            //2. 判断是否有空值
            if(cname.length() != 0 && tel.length() != 0  && sex.length() != 0  &&
                    address.length() != 0  && cpwd.length() != 0  && cpwd_assure.length() != 0 ) {

                //3. 判断两次密码是否相等
                if (cpwd.equals(cpwd_assure)) {

                    //4. 判断用户是否已存在
                    List<Customers> Cus_exist = cdao.getCustomersByTel(tel);
                    if (Cus_exist.size() == 0) {

                        //4. 加入用户
                        try{
                        	Customers cus = new Customers();
                        	cus.setCname(cname);
                        	cus.setTel(tel);
                        	cus.setSex(sex);
                        	cus.setAddress(address);
                        	cus.setCpwd(cpwd);
                            flag = cdao.insertCus(cus);
/*
 * 
 * 此处待完善“猜你喜欢”表格的数据添加
 * 							
 * 
							
                            //加入喜欢，待补充
//                            Cus_exist = cdao.Customers_By_TEL(tel);
//                            int cid = Cus_exist.get(0).getCid();
//                            for(int i = 1; i <= 5; i++) {
//                                flag = cdao.addlike(cid, i);
//                            }
*/
                            Customers cus_getTel = new Customers();
                            cus_getTel = cdao.getCusByTel(tel);
                            
                            boolean init_clike_flag = cdao.initClike(cus_getTel.getCid());
                            
                            
//                            JOptionPane.showMessageDialog(null,
//                                    "注册成功", "注册成功", JOptionPane.PLAIN_MESSAGE);		
                            request.getRequestDispatcher("successful_to_register.jsp").forward(request, response);
                            return;
                            
                        }catch (Exception ex_1){
//                            JOptionPane.showMessageDialog(null,
//                                    "未知错误" + ex_1, "未知错误" + ex_1, JOptionPane.ERROR_MESSAGE);
                            ex_1.printStackTrace();
                            request.setAttribute("fail_code", 0);
                            request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                            return;
                        }
                    } else {
//                        JOptionPane.showMessageDialog(null,
//                                "用户已存在", "用户已存在", JOptionPane.ERROR_MESSAGE);
                        request.setAttribute("fail_code", 1);
                        request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                        return;
                    }
                } else {
//                    JOptionPane.showMessageDialog(null,
//                            "密码不相等", "密码不相等", JOptionPane.ERROR_MESSAGE);
                    request.setAttribute("fail_code", 2);
                    request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                    return;
                }
            }else {
//                JOptionPane.showMessageDialog(null,
//                        "数据有误", "数据有误", JOptionPane.ERROR_MESSAGE);
                
                request.setAttribute("fail_code", 3);
                request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                return;
            }

        } catch(Exception ex_2) {
//            JOptionPane.showMessageDialog(null,
//                    "未知错误" + ex_2, "未知错误" + ex_2, JOptionPane.ERROR_MESSAGE);
            request.setAttribute("fail_code", 0);
            request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
